import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { Army, ArmyState } from '../entity/Army';
import { UserInfo } from '../common/UserInfo';
import { Soldier } from '../entity/Soldier';
import { ArmyItem } from './ArmyItem';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('ArmyList')
export class ArmyList extends Component {
    @property(Node)
    innerList: Node
    @property(Node)
    outerList: Node
    @property(Label)
    outerTittle:Label
    @property(Prefab)
    armyItem: Prefab

    private soldiersInList: Army[] = []
    private soldiersInNotArrivedList: Army[] = []
    private soldiersOutList: Army[] = []
    private soldiersOutNotArrivedList: Army[] = []

    private myArmy: Army

    async start() {
        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.init, this)

        await UserInfo.instance.refreshVillageDetail()

       
    }

    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.GetVillageDetail, this.init, this)
    }

    init(){
        this.clear()
        
        this.myArmy = Army.createArmy(Soldier.createBySoldiersString())
        this.myArmy.state = ArmyState.Stay
        this.myArmy.simZoneId = UserInfo.instance.villageData.zoneId
        this.myArmy.zoneId = this.myArmy.simZoneId
        

        for (let item of UserInfo.instance.soldiersInList) {
            this.soldiersInList.push(Army.createArmyByData(item))
        }
        for (let item of UserInfo.instance.soldiersInNotArrivedList) {
            this.soldiersInNotArrivedList.push(Army.createArmyByData(item))
        }

        for (let item of UserInfo.instance.soldiersOutList) {
            this.soldiersOutList.push(Army.createArmyByData(item))
        }

        for (let item of UserInfo.instance.soldiersOutNotArrivedList) {
            this.soldiersOutNotArrivedList.push(Army.createArmyByData(item))
        }


       
        // 自己的军队
        let node = instantiate(this.armyItem)
        this.innerList.addChild(node)
        node.getComponent(ArmyItem).setData(this.myArmy)
        // 别人支援的军队
        for(let army of this.soldiersInList){
            node = instantiate(this.armyItem)
            this.innerList.addChild(node)
            node.getComponent(ArmyItem).setData(army)
        }
        for(let army of this.soldiersInNotArrivedList){
            node = instantiate(this.armyItem)
            this.innerList.addChild(node)
            node.getComponent(ArmyItem).setData(army)
        }

        // 出征在外的军队
        for(let army of this.soldiersOutList){
            node = instantiate(this.armyItem)
            this.outerList.addChild(node)
            node.getComponent(ArmyItem).setData(army)
        }
        for(let army of this.soldiersOutNotArrivedList){
            node = instantiate(this.armyItem)
            this.outerList.addChild(node)
            node.getComponent(ArmyItem).setData(army)
        }

        if(this.soldiersOutList.length == 0 && this.soldiersOutNotArrivedList.length == 0){
            this.outerTittle.node.parent.active = false
        }
    }

    clear(){
        this.innerList.destroyAllChildren()
        this.outerList.destroyAllChildren()

        this.soldiersInList = []
        this.soldiersInNotArrivedList = []
        this.soldiersOutList = []
        this.soldiersOutNotArrivedList = []
    }

}


